package com.mine.dubbo.zookeeper;

import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;

@Configuration
@ConditionalOnProperty(prefix = "zookeeper", name = "enable", havingValue = "true")
@EnableConfigurationProperties(ZookeeperConfigProperties.class)
public class ZookeeperAutoConfiguration {
    private final ZookeeperConfigProperties zookeeperConfigProperties;

    public ZookeeperAutoConfiguration(ZookeeperConfigProperties zookeeperConfigProperties) {
        this.zookeeperConfigProperties = zookeeperConfigProperties;
    }

    @Bean
    public ZooKeeperClient zooKeeperClient() throws IOException, InterruptedException {
        return new ZooKeeperClient(zookeeperConfigProperties.getHost(), zookeeperConfigProperties.getPort());
    }
}
